﻿#define DEBUG
#define TEST

using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm.MakingBuffer
{
    class GeneralRoute : Common, IRoute
    {
        public GeneralRoute(soDatasets objDSs, Hashtable routeLnWithBfr, soStrings objStrFields)
            : base(objDSs, routeLnWithBfr, objStrFields)
        {
        }

        /// <summary>
        /// 一般的な線路に応じて、線路ラインを処理
        /// </summary>
        /// <param name="objVector">元の線路データセットベクトル</param>
        /// <param name="objRS">元の線路データレコードセット</param>
        /// <param name="objDSVsKirotei">通じているキロ程データセットベクトル</param>
        private void MakeBufferForRouteEx(soDatasetVector objVector, soRecordset objRS, soDatasetVector[] objDSVsKirotei)
        {
            feature = objRS.GetFieldValueText(RouteDataset.FLD_FEATURE_CD);
            daibun = objRS.GetFieldValueText(RouteDataset.FLD_DAIBUN_CD);
            tsuusho = objRS.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD);

            /*線路(識別子)より、該当する処理対象を見当たる*/
            string strUserId = String.Empty;
            if (tsuusho.Equals("205"))  // 北小金線の場合
            {
                strUserId = RouteDataset.FLD_USER_ID + " = 2011 and ";
            }
            else if ((feature + tsuusho).Equals("0102662")) // 成田(我孫子)線の場合、481→662一時の代わり
            {
                strUserId = RouteDataset.FLD_USER_ID + " = 2092 and ";
            }
            else if ((feature + tsuusho).Equals("0102650")) // 成田(松岸)線
            {
                strUserId = RouteDataset.FLD_USER_ID + " in (58, 60) and ";
            }
            else if ((feature + tsuusho).Equals("0102481")) // 常磐線
            {
                // 東京起点～2100
                strUserId = String.Format("({0} = {1} and {2} = '{3}' and {4} = '{5}' and {6} = '{7}'"
                    + " and (SmLength > 1000 and SmLength < 1500)) or ",
                    RouteDataset.FLD_USER_ID, "713",
                    RouteDataset.FLD_FEATURE_CD, feature,
                    RouteDataset.FLD_DAIBUN_CD, daibun,
                    RouteDataset.FLD_TSUUSHO_CD, "461");
            }

            soRecordset objSubRS = null;
            //objVector.Close();
            //objVector.ClearRecordsets();
            objSubRS = objVector.Query(
                strUserId +
                RouteDataset.FLD_FEATURE_CD + " = '" + feature + "' and " +
                RouteDataset.FLD_DAIBUN_CD + " = '" + daibun + "' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '" + tsuusho + "'",
                true, null, "");

            int multiLineCount = objSubRS.RecordCount;
            if (objSubRS != null && multiLineCount > 0)
            {
                if (!objSubRS.IsBOF())
                {
                    objSubRS.MoveFirst();
                }

                soGeoLine objLineTmp = null;
                soGeoLine[] objMultiLines = new soGeoLine[Utility.MAX_MULTIPLE_LINES]; //複数非連続線路
                ArrayList availableDSVs = new ArrayList();
                if (feature + tsuusho == "0106175"      // 横須賀線
                    || feature + tsuusho == "0102463"   // 京浜東北根岸線
                    || feature + tsuusho == "0102260")  // 中央・総武緩行線
                {
                    if (feature + tsuusho == "0102463")  // 京浜東北根岸線
                    {
                        availableDSVs.Add(objDSs["キロ標_大宮"] as soDatasetVector);
                    }
                    availableDSVs.Add(objDSs["キロ標_東京"] as soDatasetVector);
                }
                else if (feature + tsuusho == "0102481")    // 常磐線
                {
                    availableDSVs.Add(objDSs["キロ標_水戸"] as soDatasetVector);
                }

                /*線路はキロ程を通じたら、キロ程データセットベクトルを保留*/
                int i = 0;
                for (; !objSubRS.IsEOF(); objSubRS.MoveNext())
                {
                    //Console.WriteLine("feature + tsuusho :\t---" + feature + objSubRS.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD)
                    //    + "\t---" + objSubRS.GetFieldValueText(RouteDataset.FLD_USER_ID));
                    objLineTmp = objSubRS.GetGeometry() as soGeoLine;
                    if (feature + tsuusho == "0106118" && objLineTmp.Length < 3000) // 東海道貨物線
                    {
                        continue;
                    }
                    else if (feature + tsuusho == "0102481")    // 常磐線
                    {
                        if (objSubRS.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "481" 
                            && objLineTmp.Length < 104500)
                        {
                            continue;
                        }
                        // 常磐線の始端
                        else if (objSubRS.GetFieldValueText(RouteDataset.FLD_TSUUSHO_CD) == "461"
                                && objLineTmp.Length > 1000)
                        {
                            objMultiLines[i++] = objLineTmp;
                            continue;
                        }
                    }
                    for (int j = 0; objDSVsKirotei[j] != null; j++)
                    {
                        soRecordset objRSContain = 
                            objDSVsKirotei[j].QueryEx(objSubRS.GetGeometry(), seSpatialQueryMode.scsCommonPoint, "");
                        if (objRSContain != null)
                        {
                            objMultiLines[i++] = objLineTmp;
                            if (!availableDSVs.Contains(objDSVsKirotei[j]))
                            {
                                availableDSVs.Add(objDSVsKirotei[j]);
                            }
                            Marshal.ReleaseComObject(objRSContain);
                            objRSContain = null;
                            Console.WriteLine(objDSVsKirotei[j].Name + "\t\t---\t" + i.ToString() + 
                                "\t---\t" + objLineTmp.Length.ToString());
                            if (feature + tsuusho != "0102493") // 高崎線
                            {
                                break;
                            }
                        }
                    }
                }
                objSubRS.Close();
                Marshal.ReleaseComObject(objSubRS);
                objSubRS = null;

                /*複数線路の場合、始終端を触れれば接着する*/
                int newLinesCount = 0;
                soGeoLine[] objLinesDealed = new soGeoLine[Utility.MAX_MULTIPLE_LINES];
                objLinesDealed[0] = objMultiLines[0];
                if (i > 1) // 複数線路の場合
                {
                    for (int k = 1; k < i; k++)
                    {
                        if ((feature + tsuusho).Equals("0102271"))    // 八高線
                        {
                            if (objMultiLines[k - 1].Length > 30000)
                            {
                                for (int k1 = k; k1 < i; k1++)
                                {
                                    if (objMultiLines[k1].Length > 30000)
                                    {
                                        objMultiLines[k - 1].Joint(objMultiLines[k1]);
                                        objLinesDealed[newLinesCount++] = objMultiLines[k - 1];
                                        objMultiLines[k1] = null;
                                        break;
                                    }
                                }
                                if (newLinesCount > 0)
                                {
                                    break;
                                }
                            }
                        }
                        else if ((feature + tsuusho).Equals("0102664"))
                            //|| (feature + tsuusho).Equals("0106118"))   // 鹿島線
                        {
                            objMultiLines[k - 1].Joint(objMultiLines[k]);
                            objLinesDealed[newLinesCount++] = objMultiLines[k - 1];
                            objMultiLines[k] = null;
                            break;
                        }
                        else if ((feature + tsuusho).Equals("0102475")) // 山手電車線
                        {
                            if (objMultiLines[k].FindPointOnLineByDistance(objMultiLines[k].Length).x 
                                    == objMultiLines[k].FindPointOnLineByDistance(0).x
                                && objMultiLines[k].FindPointOnLineByDistance(objMultiLines[k].Length).y
                                    == objMultiLines[k].FindPointOnLineByDistance(0).y)
                            {
                                objLinesDealed[newLinesCount++] = objMultiLines[k];
                                break;
                            }
                            else
                            {
                                objMultiLines[k] = null;
                            }
                        }
                        else if ((feature + tsuusho).Equals("0106118")) // 東海道貨物線
                        {
                            if (objLinesDealed[newLinesCount].Joint(objMultiLines[k]))
                            {
                                Console.WriteLine("Joint Success!\t"
                                    + objMultiLines[k].Length.ToString() + "\t"
                                    + objLinesDealed[newLinesCount].Length.ToString());
                            }
                        }
                        else if ((feature + tsuusho).Equals("0102650")) // 成田(松岸)線
                        {
                            if (objMultiLines[k - 1].Joint(objMultiLines[k]))
                            {
                                objLinesDealed[newLinesCount++] = objMultiLines[k - 1];
                                Console.WriteLine("Joint Success!\t"
                                    + objMultiLines[k].Length.ToString() + "\t"
                                    + objLinesDealed[0].Length.ToString());
                                objMultiLines[k] = null;
                                break;
                            }
                        }
                        else if ((feature + tsuusho).Equals("0102481")) // 常磐線
                        {
                            if (objMultiLines[k - 1].Joint(objMultiLines[k]))
                            {
                                objLinesDealed[newLinesCount++] = objMultiLines[k - 1];
                                Console.WriteLine("Joint Success!\t"
                                    + objMultiLines[k].Length.ToString() + "\t"
                                    + objLinesDealed[0].Length.ToString());
                                objMultiLines[k] = null;
                                break;
                            }
                        }
                        else
                        {
                            if (objMultiLines[k] != null)
                            {
                                if (objLinesDealed[newLinesCount].FindPointOnLineByDistance(
                                        objLinesDealed[newLinesCount].Length).Equals(
                                            objMultiLines[k].FindPointOnLineByDistance(0)))
                                {
                                    if (objLinesDealed[newLinesCount].Joint(objMultiLines[k]))
                                    {
                                        Console.WriteLine("Joint Success!\t"
                                            + objMultiLines[k].Length.ToString() + "\t"
                                            + objLinesDealed[newLinesCount].Length.ToString());
                                    }
                                    Marshal.ReleaseComObject(objMultiLines[k]);
                                    objMultiLines[k] = null;
                                }
                                else
                                {
                                    objLinesDealed[++newLinesCount] = objMultiLines[k];
                                }
                            }
                        }
                    }
                }
                else
                {
                    newLinesCount++;
                }

                //複数非連続線路
                for (int k = 0; objLinesDealed[k] != null; k++)
                {
                    if (k > 0 && objLinesDealed[k].Equals(objLinesDealed[k - 1]))
                    {
                        Marshal.ReleaseComObject(objLinesDealed[k]);
                        objLinesDealed[k] = null;
                        continue;
                    }
                    Console.WriteLine(
                        String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                            RouteDataset.FLD_FEATURE_CD, feature, RouteDataset.FLD_DAIBUN_CD, daibun, RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                            k + 1, newLinesCount));

                    MakeBufferForPart(objLinesDealed[k], availableDSVs.ToArray(typeof(soDatasetVector)) as soDatasetVector[]);
                }

                Marshal.ReleaseComObject(objLineTmp);
                objLineTmp = null;
            }
        }

        #region IRoute メンバ

        /// <summary>
        /// 一般的な線路(識別子)ずつ、バッファーを作成
        /// </summary>
        /// <param name="objVector">処理元の線路データセットベクトル</param>
        public override void MakeBufferForRoute(soDatasetVector objVector)
        {
            soRecordset objRS = null;

            objVector.ClearRecordsets();
            objRS = objVector.Query(//FLD_FEATURE_CD + " <> ''",
#if TEST
            #region 指定線路別
                ////上越線
                //"(" + RouteDataset.FLD_FEATURE_CD + " in ('0103', '0106') and " +
                //RouteDataset.FLD_DAIBUN_CD + " = '01' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '495') or (" +


                ////高崎線
                //RouteDataset.FLD_FEATURE_CD + " in ('0102') and " +
                //RouteDataset.FLD_DAIBUN_CD + " = '01' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '493') ",
                  
                /*//総武快速・本線
                FLD_FEATURE_CD + " in ('0102', '0103') and " +
                FLD_DAIBUN_CD + " = '01' and " +
                FLD_TSUUSHO_CD + " in ('650', '662') ",
                 */ 
                /*//山手貨物
                FLD_FEATURE_CD + " in ('0102') and " +
                FLD_DAIBUN_CD + " = '01' and " +
                FLD_TSUUSHO_CD + " in ('465', '479') ",
                 */ 
                /*//大崎支線
                FLD_FEATURE_CD + " in ('0102') and " +
                FLD_DAIBUN_CD + " = '01' and " +
                FLD_TSUUSHO_CD + " in ('130') ",
                 */ 
                /*//常磐線
                FLD_FEATURE_CD + " in ('0102') and " +
                FLD_DAIBUN_CD + " = '01' and " +
                FLD_TSUUSHO_CD + " in ('481') ",
                 */
                /*//常磐緩行
                FLD_FEATURE_CD + " in ('0102') and " +
                FLD_DAIBUN_CD + " = '01' and " +
                FLD_TSUUSHO_CD + " in ('480') ",
                 */
            #endregion

            #region 一般線路別

            #region feature_cdは0102
                "(" +
                RouteDataset.FLD_DAIBUN_CD + " = '01' and (" +
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0102' and (1 = 2" +

                " or " +
                //埼京線
                RouteDataset.FLD_TSUUSHO_CD + " = '476'" +

                " or " +
                //東海道本線 - キロ程が付いていない(ずれ)？SplitEx→○
                RouteDataset.FLD_TSUUSHO_CD + " = '111'" +

                //" or " +
                ////中央急行線 - 中央・総武緩行線と同じ？→緩行線はコードを一時変更
                //RouteDataset.FLD_TSUUSHO_CD + " = '261'" +

                " or " +
                //中央・総武緩行線と中央急行線同じ？481→662一時の代わり
                RouteDataset.FLD_TSUUSHO_CD + " = '260'" +

                " or " +
                //山手電車線 - 順番ではない？○
                //YD-23,24？
                RouteDataset.FLD_TSUUSHO_CD + " = '475'" +

                " or " +
                //京浜東北根岸線 - 順番ではない？○
                //KE-1？
                RouteDataset.FLD_TSUUSHO_CD + " = '463'" +

                " or " +
                //五日市線 - 修正後、バグ出る○
                RouteDataset.FLD_TSUUSHO_CD + " = '268'" +

                " or " +
                //八高線 - バグ修正 → 接続している断続線路を結合 HK-8○
                RouteDataset.FLD_TSUUSHO_CD + " = '271'" +

                //" or " +
                ////山手貨物線 - 全く作成なし - キロ程(横浜)「8600, 13100, 13700」のみあり？
                //RouteDataset.FLD_TSUUSHO_CD + " = '465'" +

                //" or " +
                ////大崎支線 - 全く作成なし - 終点のキロ程なし？
                //RouteDataset.FLD_TSUUSHO_CD + " = '130'" +

                " or " +
                //東海道貨物支線 - プログラミングを見直す - KS-5分けない→KS-6なし線路おかしい？
                //文字付き
                RouteDataset.FLD_TSUUSHO_CD + " = '153'" +

                " or " +
                //横浜線
                RouteDataset.FLD_TSUUSHO_CD + " = '171'" +

                " or " +
                //鶴見線 - 修正後、バグ出る○
                RouteDataset.FLD_TSUUSHO_CD + " = '161'" +

                //" or " +
                ////南武支/尻手短絡線 - 見直す？新鶴見起点から1600まで、後はなくなった、または混雑
                //RouteDataset.FLD_TSUUSHO_CD + " = '152'" +

                " or " +
                //相模線 - キロ程(横浜)「0」なし？
                RouteDataset.FLD_TSUUSHO_CD + " = '177'" +

                " or " +
                //伊東線 - 二つの同じキロ程(横浜)「1400」あり？
                RouteDataset.FLD_TSUUSHO_CD + " = '184'" +

                //" or " +
                ////大宮支線(その１)
                //RouteDataset.FLD_TSUUSHO_CD + " = '203'" +

                " or " +
                //西浦和支線
                RouteDataset.FLD_TSUUSHO_CD + " = '204'" +

                " or " +
                //北小金支線 - SmUserID「2011」で区別できた
                RouteDataset.FLD_TSUUSHO_CD + " = '205'" +

                " or " +
                //馬橋支線
                RouteDataset.FLD_TSUUSHO_CD + " = '206'" +

                " or " +
                //東北本線 - TH-1文字付き、TH-4,5,8 キロ程(東京)が混雑(474も出ている)？
                //TH-2,3 - キロ程(東京)にない
                //TH-28 - キロ程(大宮)にもない→SplitEx○
                RouteDataset.FLD_TSUUSHO_CD + " = '461'" +

                " or " +
                //川越(貨物線含む) - 文字付き？
                RouteDataset.FLD_TSUUSHO_CD + " = '491'" +

                " or " +
                //高崎線
                RouteDataset.FLD_TSUUSHO_CD + " = '493'" +

                " or " +
                //信越本線
                RouteDataset.FLD_TSUUSHO_CD + " = '631'" +

                " or " +
                //両毛線 - 処理対象不明(九本が高崎のキロ程を通る)、マップにも見難い - プログラミングを見直す
                //キロ程(高崎)「0, 1300」なし？
                RouteDataset.FLD_TSUUSHO_CD + " = '512'" +

                " or " +
                //水戸線 - 修正後、バグ出る○
                RouteDataset.FLD_TSUUSHO_CD + " = '516'" +

                " or " +
                //吾妻線 - キロ程(高崎)「0」なし？
                RouteDataset.FLD_TSUUSHO_CD + " = '497'" +

                " or " +
                //日光線 - 修正後、バグ出る○
                RouteDataset.FLD_TSUUSHO_CD + " = '518'" +

                " or " +
                //烏山線
                RouteDataset.FLD_TSUUSHO_CD + " = '521'" +

                " or " +
                //常磐線 - 成田(我孫子)線と同じ？→我孫子はコードを一時変更○
                //"(" + RouteDataset.FLD_USER_ID + " <> 2092 and " + RouteDataset.FLD_TSUUSHO_CD + " = '481')" +
                RouteDataset.FLD_TSUUSHO_CD + " = '481'" +

                " or " +
                //常磐緩行線 - エリアの定義は？
                RouteDataset.FLD_TSUUSHO_CD + " = '480'" +

                " or " +
                //外房線 - 二つの同じキロ程(千葉)「500」あり - プログラミングを見直す
                //SB-1,2なし？
                RouteDataset.FLD_TSUUSHO_CD + " = '656'" +

                " or " +
                //内房線 - 処理対象不明(２本が千葉のキロ程を通る) - プログラミングを見直す
                //キロ程(千葉)「0」なし？
                RouteDataset.FLD_TSUUSHO_CD + " = '658'" +

                " or " +
                //京葉線 - 0101/0106×
                //キロ程ずれ？SplitEx→○
                RouteDataset.FLD_TSUUSHO_CD + " = '668'" +

                " or " +
                //成田(松岸)線
                RouteDataset.FLD_TSUUSHO_CD + " = '650'" +

                " or " +
                //成田(我孫子)線 - 常磐線と同じ? 481→662一時の代わり
                //起点不明？
                RouteDataset.FLD_TSUUSHO_CD + " = '662'" +

                " or " +
                //鹿島線 - バグ修正 → 接続している断続線路を結合 - プログラミングを見直す
                //KM-1○
                RouteDataset.FLD_TSUUSHO_CD + " = '664'" +

                " or " +
                //久留里
                RouteDataset.FLD_TSUUSHO_CD + " = '665'" +

                " or " +
                //東金 - 始端のキロ程が混雑？重複キロ程(「600」など)
                RouteDataset.FLD_TSUUSHO_CD + " = '667'" +
                " )) " +
            #endregion

            #region feature_cdは0102以外
                " or " +
                //南武線 - プログラミングを見直す - 文字付き？
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0103' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '151')" +

                //" or " +
                ////常磐貨物線 - 全く作成なし - キロ程(東京)に「0, 500, 1200, 2000, 2100」ない？
                //"(" + RouteDataset.FLD_FEATURE_CD + " = '0104' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '465')" +

                //" or " +
                ////東北本線 - 全く作成なし - 102に移した？
                //"(" + RouteDataset.FLD_FEATURE_CD + " = '0104' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '461')" +

                " or " +
                //成田(空港)線 - キロ程が付いていない(ずれ)？SplitEX→○
                //NK1？
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '661')" +

                " or " +
                //国立支線 - 見直す KN-3○
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '202')" +

                //" or " +
                ////大宮支線(その２)
                //"(" + RouteDataset.FLD_FEATURE_CD + " = '0104' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '461')" +
                //" or " +
                //"(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                //RouteDataset.FLD_TSUUSHO_CD + " = '203')" +

                " or " +
                //東北貨物線 - HF-7,8,9,10,11,12,13キロ程が付いていない(ずれ)？
                //HF-1,2,3キロ程(東京)に「7100, 7300, 8200」ない
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0104' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '465')" +

                " or " +
                //上越線
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '495')" +

                " or " +
                //横須賀線 - 順番ではない？○
                //SK-1？
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '175')" +

                " or " +
                //東海道貨物線 - 順番ではない？○
                //10.4が線路に付いていない？
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '118')" +

                #endregion

                "))",
            #endregion
#endif
                false, objStrFields,
                String.Format("group by {0}, {1}, {2} order by {0}, {1}, {2}",
                    RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_TSUUSHO_CD));

            if (objRS != null && objRS.RecordCount > 0)
            {
                soDatasetVector[] objDSVsKirotei = new soDatasetVector[Utility.MAX_KIROTEI_DATASETS];
                for (int k = 1, j = 0; k <= objDSs.Count; k++)
                {
                    if (objDSs[k].Type == seDatasetType.scdPoint)
                    {
                        if (objDSs[k].Name.Contains(Utility.KIROTEI_PREFIX))
                        {
                            objDSVsKirotei[j++] = objDSs[k] as soDatasetVector;
                        }
                    }
                }

                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if DEBUG
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    try
                    {
                        Console.WriteLine(
                            String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}",
                                RouteDataset.FLD_FEATURE_CD, objRS.GetFieldValue(RouteDataset.FLD_FEATURE_CD),
                                RouteDataset.FLD_DAIBUN_CD, objRS.GetFieldValue(RouteDataset.FLD_DAIBUN_CD),
                                RouteDataset.FLD_TSUUSHO_CD, objRS.GetFieldValue(RouteDataset.FLD_TSUUSHO_CD),
                                j + 1, objRS.RecordCount));

                        MakeBufferForRouteEx(objVector, objRS, objDSVsKirotei);
                    }
                    catch (System.Threading.ThreadAbortException ae)
                    {
                        throw ae;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);

                        objRS.Close();
                        Marshal.ReleaseComObject(objRS);
                        objRS = null;
                    }
                }

                for (int j = 0; objDSVsKirotei[j] != null; j++)
                {
                    Marshal.ReleaseComObject(objDSVsKirotei[j]);
                    objDSVsKirotei[j] = null;
                }

                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }

            if (objRS != null)
            {
                objRS.Close();
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        #endregion
    }
}
